﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface.Presentation.Controls;
using Peers = System.Windows.Automation.Peers;
using System.Windows.Automation.Peers;
using System.Windows.Interop;
using System.Diagnostics;
using System.Windows.Automation.Provider;
using System.Windows.Automation;

namespace LEETHelperControls
{
    /// <summary>
    /// Interaction logic for ScatterViewItemContentWrapper.xaml
    /// </summary>
    public partial class ScatterViewItemContentWrapper : UserControl
    {
        private ScatterViewItem Owner;
        
        public ScatterViewItemContentWrapper(ScatterViewItem parent)
        {
            Owner = parent;
            this.HorizontalAlignment=HorizontalAlignment.Stretch;
            this.VerticalAlignment=VerticalAlignment.Stretch;
            InitializeComponent();
        }

        #region automation provider hack

        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new ScatterViewItemValueHack(Owner);
        }

        class ScatterViewItemValueHack : FrameworkElementAutomationPeer, IValueProvider
        {
            #region AutomationPeer
            private double lastSeenOrientation;
            private double lastSeenWidth;
            private double lastSeenHeight;
            private double lastSeenX;
            private double lastSeenY;
            

            public ScatterViewItemValueHack(ScatterViewItem item) : base(item)
            {
                get = delegate() { return ""; };

                lastSeenOrientation = item.Orientation;
                lastSeenHeight = item.Height;
                lastSeenWidth = item.Width;
                lastSeenX = item.Center.X;
                lastSeenY = item.Center.Y;

                EventHandler<RoutedEventArgs> handler = update;
                item.ContainerManipulationCompleted += delegate(object sender, ContainerManipulationCompletedEventArgs args)
                {
                    handler(sender, args);
                };
            }

            private void update(object sender, RoutedEventArgs args)
            {
                if (Owner.Orientation != lastSeenOrientation)
                    this.RaisePropertyChangedEvent(ValuePattern.ValueProperty, lastSeenOrientation.ToString(), "_____Rotation:" + Owner.Orientation.ToString());

                if (Owner.Height != lastSeenHeight || Owner.Width != lastSeenWidth)
                    this.RaisePropertyChangedEvent(ValuePattern.ValueProperty, String.Format("{{{0}, {1}}}", lastSeenWidth, lastSeenHeight), "_____Scale:" + String.Format("{{{0}, {1}}}", Owner.Width, Owner.Height)); ;

                if (Owner.Center.X != lastSeenX || Owner.Center.Y != lastSeenY)
                    this.RaisePropertyChangedEvent(ValuePattern.ValueProperty, String.Format("{{{0}, {1}}}", lastSeenX, lastSeenY), "_____Position:" + String.Format("{{{0}, {1}}}", Owner.Center.X, Owner.Center.Y)); ;

                lastSeenOrientation = Owner.Orientation;
                lastSeenHeight = Owner.Height;
                lastSeenWidth = Owner.Width;
                lastSeenX = Owner.Center.X;
                lastSeenY = Owner.Center.Y;
            }

            protected new ScatterViewItem Owner { get { return base.Owner as ScatterViewItem; } }

            public override Object GetPattern(PatternInterface patternInterface)
            {
                if (patternInterface.Equals(System.Windows.Automation.Peers.PatternInterface.Value))
                    return this;
                if (patternInterface.Equals(System.Windows.Automation.Peers.PatternInterface.Transform))
                    return new TransformPattern(UIElementAutomationPeer.CreatePeerForElement(Owner).GetPattern(patternInterface) as ITransformProvider, this);
                
                return base.GetPattern(patternInterface);
            }

            protected override string GetClassNameCore()
            {
                return "ScatterViewItemContentWrapper";
            }

            protected override string GetAutomationIdCore()
            {
                return Owner.Name + "_AutomationHack";
            }

            protected override List<AutomationPeer> GetChildrenCore()
            {
                List<AutomationPeer> children = new List<AutomationPeer>();
                UIElement element = (Owner.Content as ScatterViewItemContentWrapper).Content as UIElement;
                if (element != null)
                {
                    AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(element);
                    if (peer != null)
                        children.Add(peer);
                }
                return children;
            }

            protected override AutomationControlType GetAutomationControlTypeCore()
            {
                return AutomationControlType.Custom;
            }
            #endregion

            #region IValueProvider
            public bool IsReadOnly
            {
                get { return false; }
            }

            private delegate String GET();
            private GET get;
            public void SetValue(string value)
            {
                //parse for property
                if ("_____Rotation".Equals(value))
                    get = delegate() { 
                        return Owner.Orientation.ToString(); 
                    };
                else if ("_____Scale".Equals(value))
                    get = delegate() { 
                        return "{" + Owner.Width.ToString() + ", " + Owner.Height.ToString() + "}"; 
                    };
                else if ("_____Position".Equals(value))
                    get = delegate() { 
                        return "{" + Owner.Center.X.ToString() + ", " + Owner.Center.Y.ToString() + "}"; 
                    };

                //set property
            }

            public string Value
            {
                get { return get(); }
            }
            #endregion

            #region ITransformProvider
            private class TransformPattern : ITransformProvider
            {
                ITransformProvider provider;
                ScatterViewItemValueHack peer;
                public TransformPattern(ITransformProvider provider, ScatterViewItemValueHack peer)
                {
                    this.provider = provider;
                    this.peer = peer;
                }

                public bool CanMove
                {
                    get { return provider.CanMove; }
                }

                public bool CanResize
                {
                    get { return provider.CanResize; }
                }

                public bool CanRotate
                {
                    get { return provider.CanRotate; }
                }

                public void Move(double x, double y)
                {
                    provider.Move(x, y);
                    peer.update(null, null);
                }

                public void Resize(double width, double height)
                {
                    provider.Resize(width,height);
                    peer.update(null, null);
                }

                public void Rotate(double degrees)
                {
                    provider.Rotate(degrees);
                    peer.update(null, null);
                }
            }
            #endregion
        }

        #endregion

        //class RotationProvider : IRotationProvider
        //{
        //    ScatterViewItem control;

        //    public int AngleInDegrees
        //    {
        //        get { return (int)control.Orientation; }
        //    }

        //    public void SetRotation(int degrees)
        //    {
        //        control.Orientation = degrees;
        //    }

        //    public void Rotate(int degrees)
        //    {
        //        control.Orientation += degrees;
        //    }
        //}
    }
}
